﻿using System.Collections.Generic;
using System.Globalization;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace EGConfig
{
    public class ExcelAnalysis
    {
        public static ConfigData GetConfigData(string filePath)
        {
            ConfigData data = new ConfigData(Path.GetFileNameWithoutExtension(filePath));

            IWorkbook workbook = new XSSFWorkbook(File.OpenRead(filePath));
            ISheet sheet = workbook.GetSheetAt(0);
            int columnCount = sheet.GetRow(0).Cells.Count;
            for (int i = 0; i < columnCount; i++)
            {
                data.propertyNames.Add(ExcelUtil.ToFirstLower(sheet.GetRow(0).GetCell(i).StringCellValue));
                data.propertyTypes.Add(new ConfigType(sheet.GetRow(1).GetCell(i).StringCellValue));
                ICell cell = sheet.GetRow(2).GetCell(i);
                string tips = cell.StringCellValue;
                if (cell.CellComment != null)
                {
                    string comment = cell.CellComment.String.String;
                    string[] arr = comment.Split('\n');
                    foreach (var c in arr)
                    {
                        if (c == "主键")
                        {
                            data.mainIndex = i;
                        }
                        else
                        {
                            tips += $" {c} ";
                        }
                    }
                }

                data.propertyTips.Add(tips);
            }

            for (int i = 3; i <= sheet.LastRowNum; i++)
            {
                List<string> rowValues = new List<string>();
                for (int j = 0; j < columnCount; j++)
                {
                    ICell cell = sheet.GetRow(i).GetCell(j);
                    if (cell == null)
                    {
                        rowValues.Add(null);
                    }
                    else
                    {
                        string v = cell.CellType == CellType.Numeric
                            ? cell.NumericCellValue.ToString(CultureInfo.InvariantCulture)
                            : cell.StringCellValue;
                        rowValues.Add(v);
                    }
                }

                data.values.Add(rowValues);
            }

            return data;
        }
    }

    public class ConfigData
    {
        public string upperName;
        public string lowerName;
        public string rawName;

        public List<string> propertyNames;
        public List<ConfigType> propertyTypes;
        public List<string> propertyTips;
        public List<List<string>> values;
        public int mainIndex = -1;

        public ConfigData(string name)
        {
            this.rawName = name;
            this.upperName = ExcelUtil.ToFirstUpper(name);
            this.lowerName = ExcelUtil.ToFirstLower(name);
            propertyNames = new List<string>();
            propertyTypes = new List<ConfigType>();
            propertyTips = new List<string>();
            values = new List<List<string>>();
        }
    }
}